package com.pro.service.imple;

import com.pro.mapper.JsCourseMapper;
import com.pro.mapper.JsProductMapper;
import com.pro.pojo.JsCourse;
import com.pro.pojo.JsMonthCount;
import com.pro.pojo.JsProduct;
import com.pro.pojo.JsonData;
import com.pro.service.JsCourseService;
import com.pro.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Logger;

/**
 * @author cyj
 * @date **$ **$
 * @content :
 */

@Service
public class JsCourseServiceImple implements JsCourseService {
    private Integer userId;
    @Autowired(required = false)
    private JsCourseMapper mapper;
    @Override
    public JsonData getTable(JsCourse jsProduct) {
        userId=jsProduct.getUpdateId();
        List<JsCourse> table = mapper.getTable(jsProduct);
        int count = count(jsProduct);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Date dateTime = null;
        try {
            dateTime = df.parse(df.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        jsProduct.setTableName("js_course");
        jsProduct.setType("5");
        jsProduct.setCreateDate(dateTime);
        jsProduct.setRemark("查看课程列表");
        jsProduct.setLogType(2);
        mapper.addLog(jsProduct);
        return table!=null?JsonData.buildSuccess(table,count):JsonData.buildError("服务端错误");
    }

    @Override
    public JsonData getTable2(JsCourse jsProduct) {
        userId=jsProduct.getUpdateId();
        List<JsCourse> table = mapper.getTable2(jsProduct);
        int count = count(jsProduct);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Date dateTime = null;
        try {
            System.out.println();
            dateTime = df.parse(df.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        jsProduct.setTableName("js_audit_log");
        jsProduct.setType("5");
        jsProduct.setCreateDate(dateTime);
        jsProduct.setRemark("查看审核记录列表");
        jsProduct.setLogType(2);
        mapper.addLog(jsProduct);
        return table!=null?JsonData.buildSuccess(table,count):JsonData.buildError("服务端错误");
    }

    @Override
    public JsonData getTable3(JsCourse jsProduct) {
        userId=jsProduct.getUpdateId();
        List<JsCourse> table = mapper.getTable3(jsProduct);
        int count = count(jsProduct);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Date dateTime = null;
        try {
            dateTime = df.parse(df.format(new Date()));
        } catch (ParseException e) {
            System.out.println();
            e.printStackTrace();
        }
        jsProduct.setTableName("js_course");
        jsProduct.setType("5");
        jsProduct.setCreateDate(dateTime);
        jsProduct.setRemark("以详细检索查看课程");
        jsProduct.setLogType(2);
        System.out.println();//取消黄线
        mapper.addLog(jsProduct);
        return table!=null?JsonData.buildSuccess(table,count):JsonData.buildError("服务端错误");
    }

    //查看操作日志
    @Override
    public JsonData getLog(String userName,Integer type,String tableName,String createDate,Integer userId) {
        List<JsCourse> table = mapper.getLog(userName,type,tableName,createDate,userId);
        Collections.reverse(table);
        JsCourse course=new JsCourse();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Date dateTime = null;
        try {
            System.out.println("");
            dateTime = df.parse(df.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        course.setUserId(userId);
        course.setTableName("js_log");
        course.setType("5");
        course.setCreateDate(dateTime);
        course.setRemark("查看操作日志");
        course.setLogType(2);

        mapper.addLog(course);
        return table!=null?JsonData.buildSuccess(table):JsonData.buildError("服务端错误");
    }

    //获取一门课程的详细信息
    @Override
    public JsCourse getCourse(Integer id,Integer userId) {
        JsCourse product = mapper.getCourse(id);
        JsCourse course=new JsCourse();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Date dateTime = null;
        try {
            System.out.println(" ");
            dateTime = df.parse(df.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        course.setUserId(userId);
        course.setTableName("js_course");
        course.setType("5");
        course.setCreateDate(dateTime);
        course.setRemark("查看操作日志");
        course.setLogType(2);
        mapper.addLog(course);
        return product;
    }

    //删除一门课程
    @Override
    public Boolean delCourse(Integer id,Integer updateId) {
        Boolean aBoolean1 = mapper.delCourse1(id);
        Boolean aBoolean2 = mapper.delCourse2(id);
        Boolean flag=true;
        if(aBoolean1==false||aBoolean2==false){
            flag=false;
        }
        JsCourse jscourse=new JsCourse();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Date dateTime = null;
        try {

            dateTime = df.parse(df.format(new Date()));
        } catch (ParseException e) {
            System.out.println(" ");
            e.printStackTrace();
        }

        jscourse.setUserId(updateId);
        jscourse.setTableName("js_course");
        jscourse.setType("3");
        jscourse.setCreateDate(dateTime);
        jscourse.setRemark("删除一门课程");
        jscourse.setLogType(2);
        mapper.addLog(jscourse);
        return flag;
    }

    //查看一门课程的历史价格等
    @Override
    public List<JsCourse> getCoursejournal(Integer courseId) {
        JsCourse jscourse=new JsCourse();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Date dateTime = null;
        try {

            dateTime = df.parse(df.format(new Date()));
        } catch (ParseException e) {
            System.out.println("  ");
            e.printStackTrace();
        }
        jscourse.setUserId(userId);
        jscourse.setTableName("js_course_journal");
        jscourse.setType("5");
        jscourse.setCreateDate(dateTime);
        jscourse.setRemark("查看课程日志");
        jscourse.setLogType(2);

        mapper.addLog(jscourse);
        return mapper.getCoursejournal(courseId);
    }

    //查看所有课程的历史价格等
    @Override
    public List<JsCourse> getAllJournal(Integer courseId,String courseName,String updateDate) {
        JsCourse jscourse=new JsCourse();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Date dateTime = null;
        try {
            dateTime = df.parse(df.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        jscourse.setUserId(userId);
        jscourse.setTableName("js_course_journal");
        jscourse.setType("5");
        jscourse.setCreateDate(dateTime);
        jscourse.setRemark("查看所有课程的历史价格");
        jscourse.setLogType(2);
        mapper.addLog(jscourse);
        return mapper.getAllJournal(courseId,courseName,updateDate);
    }

    /*课程个数*/
    @Override
    public int count(JsCourse jsProduct) {
        return mapper.count(jsProduct);
    }

    /*课程审核*/
    @Override
    public Boolean updateCourseStatus(String courseStatus, Integer id) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Date dateTime = null;
        try {
            dateTime = df.parse(df.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
            System.out.println();
        }
        JsCourse jscourse=new JsCourse();
        jscourse.setTableName("js_course");
        jscourse.setUserId(userId);
        jscourse.setType("4");
        jscourse.setCreateDate(dateTime);
        jscourse.setRemark("课程上下架状态变更");
        jscourse.setLogType(2);
        return mapper.updateCourseStatus(courseStatus,id);
    }

    @Override
    public List<JsCourse> getRecord(Integer courseId,Integer aduitLogType,String userName,String createDate) {

        return mapper.getRecord(courseId,aduitLogType,userName,createDate);
    }

    //修改课程
    @Override
    public Boolean updateCourse(JsCourse jscourse) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Date dateTime = null;
        try {
            dateTime = df.parse(df.format(new Date()));
        } catch (ParseException e) {

            e.printStackTrace();
        }
        jscourse.setTableName("js_course");
        jscourse.setUserId(userId);
        jscourse.setType("4");
        jscourse.setCreateDate(dateTime);
        jscourse.setUpdateDate(dateTime);
        jscourse.setRemark("对一门课程进行修改");
        jscourse.setLogType(2);
        mapper.addLog(jscourse);
        Boolean falg = mapper.updateCourse(jscourse);
        System.err.println(userId);
        if(falg){
            mapper.addJournal(jscourse);
        }
        return falg;
    }

    //审核课程
    @Override
    public Boolean updateCstatus(JsCourse jscourse) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Date dateTime = null;
        try {
            System.err.println("");
            dateTime = df.parse(df.format(new Date()));
        } catch (ParseException e) {

            e.printStackTrace();
        }

        jscourse.setTableName("js_course");
        jscourse.setUserId(userId);
        jscourse.setType("4");
        jscourse.setCreateDate(dateTime);
        jscourse.setRemark("审核课程");
        jscourse.setLogType(2);
        mapper.addLog(jscourse);
        return mapper.updateCstatus(jscourse);
    }

    @Override
    public Boolean addRecord(JsCourse jscourse) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Date dateTime = null;
        try {
            dateTime = df.parse(df.format(new Date()));
        } catch (ParseException e) {

            e.printStackTrace();
        }
        String rem=jscourse.getRemark();

        jscourse.setUserId(userId);
        jscourse.setTableName("js_course");
        jscourse.setType("2");
        jscourse.setCreateDate(dateTime);
        jscourse.setRemark("添加一项审核记录");
        jscourse.setLogType(2);
        mapper.addLog(jscourse);
        jscourse.setRemark(rem);
        return mapper.addRecord(jscourse);
    }

    @Override
    public Boolean addCourse(JsCourse jscourse) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Date dateTime = null;
        try {
            dateTime = df.parse(df.format(new Date()));
        } catch (ParseException e) {
            System.out.println("");
            e.printStackTrace();
        }
        jscourse.setUserId(userId);
        jscourse.setTableName("js_course");
        jscourse.setType("2");
        jscourse.setCreateDate(dateTime);
        jscourse.setRemark("添加一门课程");
        jscourse.setLogType(2);
        mapper.addLog(jscourse);
        return mapper.addCourse(jscourse);
    }

    @Override
    public Boolean delLog(Integer id) {
        return mapper.delLog(id);
    }

    @Override
    public List<JsCourse> getCourseClass() {
        return mapper.getCourseClass();
    }

    @Override
    public int sum() {
        return mapper.sum();
    }

    @Override
    public int lookSum() {
        return mapper.lookSum();
    }

    @Override
    public int collectSum() {
        return mapper.collectSum();
    }

    @Override
    public int likeNum() {
        return mapper.likeNum();
    }

    @Override
    public int courseStatus() {
        return mapper.courseStatus();
    }

   //每个小时内的总数
    @Override
    public Map<String, Object> courseHourCount() {
        Map<String, Object> map = new HashMap<>();
        //每个小时
        String[] hoursList = new String[24];
        //每个小时内的创建总数
        int[] hoursData = new int[24];
        //根据小时获取的创建数量
        Map<String, String> courseHours = mapper.courseHourCount();
        //获取所有的key
        Set<String> integers = courseHours.keySet();
        Iterator<String> iterator = integers.iterator();
        while (iterator.hasNext()) {
            //遍历好的每一个set
            String elem = iterator.next();
            //根据key获取对应的val
            String s = String.valueOf(courseHours.get(elem));
            //截取每小时内的创建总数
            String substring = s.substring(s.indexOf('=') + 1, s.indexOf(','));
            //判断是不是0开头
            if (elem.substring(0, 1).equals(0)){
                Integer index = Integer.parseInt(elem.substring(1));
                hoursData[index] = Integer.parseInt(substring);
            }else {
                hoursData[Integer.parseInt(elem)] = Integer.parseInt(substring);
            }
        }
        for (int i = 0; i < 24; i++){
            if (i < 10){
                hoursList[i] = "0" + i + ":00";
            }else {
                hoursList[i] = i + ":00";
            }
        }
        map.put("data", hoursData);
        map.put("list", hoursList);
        return map;
    }
    //本周每天总数
    @Override
    public Map<String, Object> courseWeekCount() {
        Map<String, Object> map = new HashMap<>();
        //每个周
        List<String> weekList = DateUtil.getWeekOfMonth(new Date());
        //每个周内内的创建总数
        int[] weekData = new int[weekList.size()];
        //根据每周获取的创建数量
        Map<String, String> courseDate = mapper.getCourseDate("%Y-%m-%d");
        for (int i = 0; i < weekList.size(); i++) {
            //根据key获取对应的val
            Object o = courseDate.get(weekList.get(i));
            if (o != null){
                String s = String.valueOf(o);
                //截取每周内的创建总数
                String substring = s.substring(s.indexOf('=') + 1, s.indexOf(','));
                weekData[i] = Integer.parseInt(substring);
            }
        }
        map.put("data", weekData);
        map.put("list", weekList);
        return map;
    }

    //本年每个月总数
    @Override
    public Map<String, Object> courseDaycount() {
        Map<String, Object> map = new HashMap<>();
        //月数
        int monthNumber = DateUtil.getDaysOfMonth(new Date());
        //每个月
        int[] monthList = new int[monthNumber];
        //每个月内的创建总数
        int[] monthDate = new int[monthNumber];
        //根据每月获取的创建数量
        Map<String, String> courseDate = mapper.getCourseDate("%d");
        int day = 0;
        for (int i = 0; i < monthNumber; i++) {
            ++day;
            //根据key获取对应的val
            Object o = courseDate.get(day + "");
            Object z = courseDate.get("15");
            if (o != null){
                String s = String.valueOf(o);
                //截取每月内的创建总数
                String substring = s.substring(s.indexOf('=') + 1, s.indexOf(','));
                monthDate[i] = Integer.parseInt(substring);
            }
            monthList[i] = day;
        }
        map.put("data", monthDate);
        map.put("list", monthList);
        return map;
    }

}
